//
//  AIPlayer.h
//  CAIPlayer: artifical intelligence player
//
//  Created by Enric Vergara on 20.10.12. 
//  Copyright OWLAB 2010. All rights reserved.

#import "AIPlayer.h"


@implementation CAIPlayer


-(id) init
{
    if( (self=[super init])) 
    {
        m_eTypePlayer = PLAYER_AI;
        //Cargo las imagenes ...
        
        CCLabel *label = [CCLabel labelWithString:NSLocalizedString(@"Iam_AI_Player", @"") fontName:@"Arial" fontSize:24];
        [label setColor:ccc3(0,0,0)];
        [label setPosition:CGPointMake(0, 90)];
        [self addChild: label];
        m_bOnEnterFirstTime = true;
    }
    return self;
}


- (void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
}


- (bool) check:(unsigned int) currentBet deltaTime:(ccTime)delta firstBet:(bool) flag_firstBet
{
    //------------------Primer de tot mirar si els jugadors son delear, smallBlend o bigBlend----
    if (flag_firstBet)
    {
        return [self checkFirstBet:delta];
    }
    //---------------------------------------------------------------------------------------------
    
    
    m_fCounterTime += delta;
    bool isEndBet = false;
    
    if (m_bOnEnterFirstTime)
    {
        m_uiToBank = 0;
        m_uiCall = 0;
        m_uiRaise = 0;
        
        m_bOnEnterFirstTime = false;
        [self searchOptions:currentBet];
        [self setPlayerState:PlayerState_Thinking];
    }
    else
    {
        if (m_fCounterTime >= AI_PLAYER_THINKING_TIME)
        {
            m_bOnEnterFirstTime = true;
            isEndBet = true;
            m_fCounterTime = 0.f;
            
            //fer un random--> 50% de vegades check 50% raise (5 eurets+)
            int random = (int) (arc4random() % 10);
            if( random == 0)
            {
                //Un 10% de las veces vamos a hacer un fold o en su defecto check
                if (m_CheckOptions.m_bDoFold)
                {
                    [self setPlayerState:PlayerState_Fold];
                }
                else if (m_CheckOptions.m_bDoCheck)
                {
                    [self setPlayerState:PlayerState_Fold];
                }
                else
                {
                    //allIn
                    m_uiCall = m_CheckOptions.m_uiCallAllIn;
                    m_uiRaise = m_CheckOptions.m_uiRaiseAllIn;
                    [self setPlayerState:PlayerState_AllIn];
                }
            }
            else if (random < 6)
            {
                //Un 50% de las veces vamos a hacer un check, en su defecto un call y si no se puede un allin
                if (m_CheckOptions.m_bDoCheck)
                {
                    [self setPlayerState:PlayerState_Check];
                }
                else if (m_CheckOptions.m_bDoCall)
                {
                    m_uiCall = m_CheckOptions.m_uiCallAmmount;
                    [self setPlayerState:PlayerState_Call];
                }
                else
                {
                    //allIn
                    m_uiCall = m_CheckOptions.m_uiCallAllIn;
                    m_uiRaise = m_CheckOptions.m_uiRaiseAllIn;
                    [self setPlayerState:PlayerState_AllIn];
                }
                
            }
            else if (random < 8)
            {
                //Un 20% de las veces vamos a hacer un call, en su defecto un check sino un all in
                if (m_CheckOptions.m_bDoCall)
                {
                    m_uiCall = m_CheckOptions.m_uiCallAmmount;
                    [self setPlayerState:PlayerState_Call];
                }
                else if(m_CheckOptions.m_bDoCheck)
                {
                    [self setPlayerState:PlayerState_Check];                    
                }
                else
                {
                    //allIn
                    m_uiCall = m_CheckOptions.m_uiCallAllIn;
                    m_uiRaise = m_CheckOptions.m_uiRaiseAllIn;
                    [self setPlayerState:PlayerState_AllIn];
                }
            }
            else if (random < 9)
            {
                //Un 10% de las veces vamos a hacer un raise, en su defecto un all in
                if (m_CheckOptions.m_bDoRaise)
                {
                    m_uiCall = m_CheckOptions.m_uiCallAmmount;
                    m_uiRaise = m_CheckOptions.m_uiRaiseAmmount;
                    [self setPlayerState:PlayerState_Raise];
                    
                }
                else
                {
                    //allIn
                    m_uiCall = m_CheckOptions.m_uiCallAllIn;
                    m_uiRaise = m_CheckOptions.m_uiRaiseAllIn;
                    [self setPlayerState:PlayerState_AllIn];
                }
            }
            else
            {
                //Un 10% de las veces vamos a hacer un all in
                m_uiCall = m_CheckOptions.m_uiCallAllIn;
                m_uiRaise = m_CheckOptions.m_uiRaiseAllIn;
                [self setPlayerState:PlayerState_AllIn];
            }

        } //END if (m_fCounterTime >= AI_PLAYER_THINKING_TIME)
    }
    
    return isEndBet;

}


@end